package com.infonova.education.microservice.controller;

import com.infonova.education.microservice.repository.entity.Product;
import com.infonova.education.microservice.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Iterator;


@RestController
public class ProductController {

    @Autowired
    private ProductService productService;

    @RequestMapping(value = "/products/createProduct/{name}", method = RequestMethod.PUT)
    public @ResponseBody
        double addProduct(@PathVariable String name) {
        Product p =  new Product();
        p.setId(productService.nextID());
        p.setName(name);
        System.out.println("Test: "+ name);
        productService.createProduct(p);
        return p.getId();
    }
        
        @RequestMapping(value = "/products/add/{name}", method = RequestMethod.GET)
    public @ResponseBody
        double adProduct(@PathVariable String name) {
        Product p =  new Product();
        p.setId(productService.nextID());
        p.setName(name);
        System.out.println("Test: "+ name);
        productService.createProduct(p);
        return p.getId();
    }
    
    @RequestMapping(value = "/products/updateProduct/{id},{name}", method = RequestMethod.PUT, headers="{Accept=*/*, content-type=multipart/form-data}")
    public @ResponseBody Product updateProduct(@PathVariable long id, @PathVariable String name) {
        Product p = productService.getProduct(id);
        p.setName(name);
        productService.updateProduct(id, p);
        return productService.getProduct(id);
    }
    
    @RequestMapping("/products/allProducts")
    public Iterable<Product> allProducts() {
        return productService.getAllProducts();
    }

   
    //@PathVariable
    //oder @RequestParam("id")
    @RequestMapping(value = "/products/getProduct/{id}", method = RequestMethod.GET)
    public @ResponseBody Product getProduct(@PathVariable long id) {
        return productService.getProduct(id);
    }
    
    
    
    @RequestMapping(value = "/products/deleteProduct", method = RequestMethod.DELETE)
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void deleteProduct(@RequestBody long id) {
        productService.deleteProduct(id);
    }
    
    @RequestMapping(value = "/products/allowedProducts", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void allowedProducts(@RequestBody int age, @RequestBody String locale) {
        productService.getAllowedProducts(age, locale);
    }

}
